﻿using System;
using System.Collections.Generic;
using System.Text;
using RFIDStockManagement.Model.Public;
using System.Data;

namespace RFIDStockManagement.DAL.Reports
{
    public class ReportInventory:DBReportBase
    {
        private const string SQL_QUERRY_INVENTORY = @"Select t2.FName as FitemName, t2.FNumber as FitemNumber,t3.FName as FStock,
t4.FName as FUnit, Round(t1.FQty/t4.FRate,4) as FCurrQty,Round(t2.FSafeStock,4) as FSafeQty
From rfid_Inventory t1
Inner Join  rfid_Item t2 on t1.FItemID = t2.Fid
Inner Join rfid_Stock t3 on t1.FStockID = t3.Fid
Inner Join rfid_Measure t4 on t2.FStockMeasureID = t4.Fid";

        public ReportInventory()
        {
            base.ReportCaption = "即时库存";
            base.ReportKey = "Inventory";
            InitColumns();
        }


        private void InitColumns()
        {
            ReportColumn newCol;
            ReportColumn newCol2;
            newCol = AddColumn("FitemName", "物料名称");
            newCol.Index = 1;

            newCol = AddColumn("FitemNumber", "物料代码");
            newCol.Index = 2;

            newCol = AddColumn("FStock", "仓库名称");
            newCol.Index = 3;

            newCol = AddColumn("FUnit", "单位名称");
            newCol.Index = 4;
            newCol.FormateString = "0.0000";

            newCol = AddColumn("FQty", "数量");
            newCol.Index = 5;


            newCol2 = new ReportColumn("FCurrQty", "即时库存量");
            newCol2.FormateString = "0.000";
            newCol2.ColumnType = RFIDStockManagement.Common.Public.Enums.EnumDataTypes.TNumeric;
            newCol.SonColumns.Add(newCol2);

            newCol2 = new ReportColumn("FSafeQty", "安全库存量");
            newCol2.FormateString = "0.000";
            newCol2.ColumnType = RFIDStockManagement.Common.Public.Enums.EnumDataTypes.TNumeric;
            newCol.SonColumns.Add(newCol2);

        }

        public override System.Data.DataTable GetReportData()
        {
            string sql = SQL_QUERRY_INVENTORY + GetFilterString();


            return GetData(sql);
        }


        private string GetFilterString()
        {
            string retWhere = " Where 1=1";
            string testValue;

            if (Filters == null) return string.Empty;

            if (Filters.Keys.Count <= 0) return string.Empty;

            testValue = GetFilterData("FItemStart");

            if (!string.IsNullOrEmpty(testValue))
            {
                retWhere += " And t2.FName >='" + testValue + "' ";
            }

            testValue = GetFilterData("FItemEnd");

            if (!string.IsNullOrEmpty(testValue))
            {
                retWhere += " And t2.FName <='" + testValue + "' ";
            }

            return retWhere;
        }

    }
}
